package com.cloudlead.common.lang.utils;

import java.sql.Timestamp;
import java.util.Date;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.SqlTimestampConverter;

public class DateTimeConvertUtils {
	static {
		SqlTimestampConverter sqlTimestampConverter = new SqlTimestampConverter();
		sqlTimestampConverter.setPatterns(new String[] { "yyyy-MM-dd",
				"yyyy-MM-dd HH:mm", "yyyy-MM-dd HH:mm:ss",
				"yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd HH:mm:ss.S",
				"yyyy-MM-dd'T'HH:mm:ss.S" });
		ConvertUtils.register(sqlTimestampConverter, Timestamp.class);
		ConvertUtils.register(sqlTimestampConverter, Date.class);

	}

	public static Timestamp convertToTimestamp(Object obj) {
		Timestamp convert = (Timestamp) ConvertUtils.convert(obj,
				Timestamp.class);
		return convert;
	}

	public static Date convertToDate(Object obj) {
		Date convert = (Date) ConvertUtils.convert(obj, Date.class);
		return convert;
	}

	public static String convertToString(Object obj) {
		String convert = (String) ConvertUtils.convert(obj, String.class);
		return convert;
	}
}
